<?php

namespace Home\Controller;
use core\lib\Exception;
use Home\Org\Util\Sqlserver;
use Org\Util\UploadFile;
/**
 * 售前 售后服务
 * @author dem
 *
 */
class ExpexceptionController extends HomeController {
    

    



    
    function initialize() {
        require 'Application/Home/ORG/Util/Sqlserver.class.php';
    }
    



    function explist(){
        $this->display();
    }

    public function getlist()
    {
        $result=array('data'=>array());
        Vendor('Util.TService');
        import("@.ORG.Util.Sqlserver");
        $page = I('pageIndex')+1;

        $pageSize = I('pageSize')?:50;
        $expNo = I('expNo')?:'';
        //echo $expNo;exit;
        $url="http://192.168.1.177:81/exp/?service=Express.AnomalyList";
        $data=array('expNo'=>$expNo,'page'=>$page,'pageSize'=>$pageSize);
        $res = \TService::instance()->curl_request($url,$data);
        //$sql="select U_OrderId,U_ItemCode,U_WhsName,U_CitemNm from [@ti_z0081] where U_ExpNo=''";
        //echo "<pre>";
        //print_r($result['data']=$res['data']['data']);
        $expList='';
        if($res['ret']==200)
        {
            //$result['data']=$res['data']['data'];
            $result['total']=$res['data']['total'];
            $arr=array();
            foreach($res['data']['data'] as $k=>$v)
            {
                $expList.="'".$v['expNo']."',";
                $arr[$v['expNo']]=$v;
            }
            $expList= rtrim($expList, ',');
            $sql="select  FreeTxt,CardCode,ShortName,itemcode,ItemName,u_brand, a.U_WhsCode,a.U_WhsName, a.u_expno from  [@ti_z0081] a join AX_ReportXp on a.U_OrderId=FreeTxt  where a.u_expno in($expList)";
            //echo $sql;exit;
            $sqlser = new Sqlserver();
            $orderList = $sqlser->query($sql);
            foreach($orderList as $key=>$value)
            {
                $arr[$value['u_expno']]=array_merge($arr[$value['u_expno']],$value);

            }
            $result['data']=array_values($arr);
        }
        //echo "<pre>";
        //print_r($result['data']);exit;
        echo json_encode($result);exit;


    }
    public function download()
    {
        set_time_limit(0);
        ini_set ('memory_limit', '512M') ;
        $result=array('data'=>array());
        Vendor('Util.TService');
        import("@.ORG.Util.Sqlserver");
        $page=1;
        $pageSize =100;
        $expNo = I('expNo')?:'';

        $url="http://192.168.1.177:81/exp/?service=Express.AnomalyList";
        $data=array('expNo'=>$expNo,'page'=>$page,'pageSize'=>$pageSize);
        $res = \TService::instance()->curl_request($url,$data);
        $expList='';
        $arr=array();
        if($res['ret']==200)
        {
            for($j=2;$j<=ceil($res['data']['total']/$pageSize);$j++)
            {
                //echo "---".$j."<br>";
                $post_data=array('expNo'=>$expNo,'page'=>$j,'pageSize'=>$pageSize);
                $ress = \TService::instance()->curl_request($url,$post_data);
                if($ress['ret']==200)
                {
                    $res['data']['data']=array_merge($res['data']['data'],$ress['data']['data']);
                }
            }
            foreach($res['data']['data'] as $k=>$v)
            {
                $expList.="'".$v['expNo']."',";
                $arr[$v['expNo']]=$v;
            }
            $expList= rtrim($expList, ',');
            $sql="select  FreeTxt,CardCode,ShortName,itemcode,ItemName,u_brand, a.U_WhsCode,a.U_WhsName, a.u_expno from  [@ti_z0081] a join AX_ReportXp on a.U_OrderId=FreeTxt  where a.u_expno in($expList)";
            $sqlser = new Sqlserver();
            $orderList = $sqlser->query($sql);
            foreach($orderList as $key=>$value)
            {
                $arr[$value['u_expno']]=array_merge($arr[$value['u_expno']],$value);

            }
            $result['data']=array_values($arr);

            $result['total']=$res['data']['total'];

        }
        //echo "<pre>";
        //print_r($result);exit;
        Vendor('Excel.PHPExcel');
        $objPHPExcel = new \PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1','订单号')
            ->setCellValue('B1','平台名称')
            ->setCellValue('C1','品牌')
            ->setCellValue('D1','物料编号')
            ->setCellValue('E1','物料名称')
            ->setCellValue('F1','仓库名称')
            ->setCellValue('G1','快递单号')
            ->setCellValue('H1','快递名称')
            ->setCellValue('I1','状态')
            ->setCellValue('J1','开始时间')
            ->setCellValue('K1','开始信息')
            ->setCellValue('L1','结束时间')
            ->setCellValue('M1','结束信息')
           ;
        $i=2;                //定义一个i变量，目的是在循环输出数据是控制行数
        $objPHPExcel->getActiveSheet()->getStyle('D1')->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
        $objPHPExcel->getActiveSheet()->getStyle('G1')->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
        foreach ($result['data'] as $key=>$v){
            $objPHPExcel->getActiveSheet()->setCellValueExplicit('A' . $i, $v['product_quality'], \PHPExcel_Cell_DataType::TYPE_STRING);
            $objPHPExcel->getActiveSheet()->setCellValueExplicit('C' . $i, $v['stock_number'], \PHPExcel_Cell_DataType::TYPE_STRING);
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue("A".$i, $v['FreeTxt']." ")
                ->setCellValue("B".$i, mb_convert_encoding($v['ShortName'],'GBK',mb_detect_encoding($v['ShortName'],array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'))))
                ->setCellValue("C".$i, mb_convert_encoding($v['u_brand'],'GBK',mb_detect_encoding($v['u_brand'],array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'))))
                ->setCellValue("D".$i, $v['itemcode'])
                ->setCellValue("E".$i, mb_convert_encoding($v['ItemName'],'GBK',mb_detect_encoding($v['ItemName'],array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'))))
                ->setCellValue("F".$i, mb_convert_encoding($v['U_WhsName'],'GBK',mb_detect_encoding($v['U_WhsName'],array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'))))
                ->setCellValue("G".$i, $v['expNo']." ")
                ->setCellValue("H".$i, $v['expCom'])
                ->setCellValue("I".$i, $v['expStatus']." ")
                ->setCellValue("J".$i, $v['start'])
                ->setCellValue("K".$i, $v['startInfo'])
                ->setCellValue("L".$i, $v['end'])
                ->setCellValue("M".$i, $v['endInfo']);
            $i++;
        }
        $objPHPExcel->getActiveSheet()->setTitle('异常快递列表');
        $objPHPExcel->setActiveSheetIndex(0);
        $filename='EXP_exception'. date('YmdHis',time());
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '.xls"');
        header('Cache-Control: max-age=0');
        $objWriter->save('php://output');


    }
    public function updateState()
    {
        Vendor('Util.TService');
        $url="http://192.168.1.177:81/exp/?service=Express.signin";
        $expNo = I('expNo')?:'';
        $data=array('expNo'=>$expNo);
        $res = \TService::instance()->curl_request($url,$data);
        if($res['ret']==200)
        {
            $result['data']=$res['data'];
        }
        echo json_encode($result);
        exit;
    }




}